package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.exception.LeaseStatusException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.lease.model.entity.SystemPost;
import com.atguigu.lease.web.admin.service.SystemPostService;
import com.atguigu.lease.web.admin.mapper.SystemPostMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author liubo
* @description 针对表【system_post(岗位信息表)】的数据库操作Service实现
* @createDate 2023-07-24 15:48:00
*/
@Service
public class SystemPostServiceImpl extends ServiceImpl<SystemPostMapper, SystemPost>
    implements SystemPostService{

    // 不用我们写代码,注入service和mapper都一样
    //如果要写代码,注入mapper方便一些
    @Autowired //查用户把用户注入进来
    private SystemUserService systemUserService;

    @Override
    public void customRemoveById(Long id) {
        //查询是否有岗位对应的用户(先检查是否存在用户信息)
        //        <SystemUser> 放用户表
        LambdaQueryWrapper<SystemUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //               判断 用户表的post_id字段 与 当前岗位的id是否相等
        lambdaQueryWrapper.eq(SystemUser::getPostId, id);
        long count = systemUserService.count(lambdaQueryWrapper);
        //有，不能直接删除
        if (count > 0) {
            throw new LeaseStatusException(ResultCodeEnum.DELETE_ERROR.getMessage(), ResultCodeEnum.DELETE_ERROR.getCode());
        }
        // 没有，正常删除岗位信息
        removeById(id);

    }
}




